// $('.main_content').find('.lds-css')
var url = {
    base:'http://119.23.232.192:8081/',
    // base:'http://10.87.140.204:8081/',
    login:'Organization/user/login',
    count:'Grid/Count/',
    countD:'Grid/Down/',
    countQ:'Grid/Quantification/',
    grid:'Organization/grid/grid_choice_list',
    post:'Organization/post/',
    dep:'department_list',
    deviceType:'Device/device/type_list',
    Fdep:'Organization/department/department_list',  
}
// var url = {
//     gb:'./resource/json/integrative.json',
//     device:'http://119.23.232.192:8081/Grid/Count/device_report',
//     info:'http://119.23.232.192:8081/Grid/Count/information_report'
// }
//tab菜单
$(function(){
    $(document).click(function(){
        $(".pullDown_ul2").hide();
        $(".pullDown_li11").removeClass("pullDown_li_click");
        $(".pullDown_ul3").hide();
        $(".pullDown_li2").removeClass("pullDown_li_click");
        $(".pullDown_ul4").hide();
        $(".pullDown_li3").removeClass("pullDown_li_click");
    })
    $(".pullDown_li11").click(function(event){
        event.stopPropagation();
        $(".pullDown_ul3").hide();
        $(".pullDown_ul4").hide();
        $(".pullDown_li2").removeClass("pullDown_li_click");
        $(".pullDown_li3").removeClass("pullDown_li_click");
        if($(".pullDown_ul2").is(":hidden")){
            $(".pullDown_ul2").show();
            $(".pullDown_li11").addClass("pullDown_li_click");
        }else{
            $(".pullDown_ul2").hide();
            $(".pullDown_li2").removeClass("pullDown_li_click");
        }
    })
    $(".pullDown_li2").click(function(event){
        event.stopPropagation();
        $(".pullDown_ul2").hide();
        $(".pullDown_ul4").hide();
        $(".pullDown_li11").removeClass("pullDown_li_click");
        $(".pullDown_li3").removeClass("pullDown_li_click");
        if($(".pullDown_ul3").is(":hidden")){
            $(".pullDown_ul3").show();
            $(".pullDown_li2").addClass("pullDown_li_click");
        }else{
            $(".pullDown_ul3").hide();
            $(".pullDown_li2").removeClass("pullDown_li_click");
        }
    })
    $(".pullDown_li3").click(function(event){
        event.stopPropagation();
        $(".pullDown_ul2").hide();
        $(".pullDown_ul3").hide();
        $(".pullDown_li11").removeClass("pullDown_li_click");
        $(".pullDown_li2").removeClass("pullDown_li_click");
        if($(".pullDown_ul4").is(":hidden")){
            $(".pullDown_ul4").show();
            $(".pullDown_li3").addClass("pullDown_li_click");
        }else{
            $(".pullDown_ul4").hide();
            $(".pullDown_li3").removeClass("pullDown_li_click");
        }
    })
    $(".pullDown_ul2_ul1_li").on("mouseover",function(){
        $(".pullDown_ul2_ul1").show()
    })
    $(".pullDown_ul2_ul1_li").on("mouseout",function(){
        $(".pullDown_ul2_ul1").hide()
    })
});
$('.main_content').css('height',$('html').height()-$('.searchwrap').outerHeight()-10)
$('.main_content').css('width',$('html').width())
//图形、表格、导出切换
$('.tab_li').on('click',function() {
    var i = $(this).index();//下标第一种写法
    $(this).addClass("tab_li_click").siblings().removeClass("tab_li_click")
    $('.nan').eq(i).show().siblings('.nan').hide();
});
// 房修和信息切换
$(".li_change").click(function(){
    var i=$(this).index();
    $(this).addClass("li_change_color").siblings().removeClass("li_change_color");
})
// 下拉菜单
$(document).ready(function(){
    var $category = $('.count_classify label:gt(4):not(:last)');
    $category.hide();
    var $toggleBtn = $('.more_a');
    var $toggleDown1=$(".down1");
    var $toggleDown2=$(".down2");
    // $toggleBtn.on('click',function(){
    //     var that=$(this);
    //     if(that.hasClass("down_img")){
    //         $category.show();
    //         that.removeClass("down_img").addClass("down_img1")
    //     }else if(that.hasClass("down_img1")){
    //         $category.hide()
    //         that.removeClass("down_img1").addClass("down_img");
    //     }
    // })
    $toggleBtn.toggle(function(){
        $('.count_classify').removeClass('up')
        $category.show();
        $(this).find('a').removeClass('hide').addClass('show');
        $toggleDown1.hide();
        $toggleDown2.show();
    },function(){
        $('.count_classify').addClass('up')
        $category.hide();
        $(this).find('a').removeClass('show').addClass('hide');
        $toggleDown2.hide();
        $toggleDown1.show();
        
    });
    $("#lookUp").click(function(){
        $category.hide();
        $toggleDown2.hide();
        $toggleDown1.show();
    })
});
//时间选择及，默认
$("#datetimepicker").datetimepicker({
    timepicker: false,
    format:'Y-m-d'
});
$("#datetimepicker2").datetimepicker({
    timepicker: false,
    format:'Y-m-d'
})
function newTime(type){
    var nowTime = new Date();
    var days = String(nowTime.getDate()).length==1?'0'+nowTime.getDate():nowTime.getDate();   
    var years = nowTime.getFullYear();
    var months = String(nowTime.getMonth()+1).length ==1?'0'+(nowTime.getMonth()+1):(nowTime.getMonth()+1);
    var foemaTime = years+'-'+months+'-'+days;
    if(arguments[0] && arguments[0] == 'time1'){
        $('input[name=ptime_start]').val(years+'-'+months+'-01');
        $('input[name=ptime_end]').val(foemaTime);
    }else if(arguments[0] && arguments[0] == 'time2'){
        var _hour = nowTime.getHours()
        //console.log(_hour)
        $('input[name=ptime_start]').val(foemaTime+' '+_hour+':00');
        $('input[name=ptime_end]').val(foemaTime+' '+(_hour+1)+':00');
    }else{
        $('input[name=ptime_start]').val(foemaTime);
        $('input[name=ptime_end]').val(foemaTime);
    }
    $('.startDate').text(foemaTime);
    $('.endDate').text(foemaTime);
}newTime();
// 下拉2
//动态显示当前月
function time(){
    //设置月
    document.getElementById("select_year")[2].selected=true;
    var select_quarter=document.getElementById("select_quarter");
    var select_month=document.getElementById("select_month");
    var d=new Date();
    // var year=d.getFullYear();
    // console.log(year)
    for(var i=2017;i<2030;i++){
        var option3=document.createElement("option");
        option3.setAttribute("value",i);
        option3.innerHTML=i;
        var year=d.getFullYear();
        select_month.appendChild(option3);
        $("#select_month").find("option[value='"+year+"']").attr("selected","selected")
    }
    // select_month.appendChild(year);
    for(var i=1;i<13;i++){
        var select_option_month=document.createElement("option");
        select_option_month.setAttribute("value",i);
        select_option_month.innerHTML=i+"月";
        select_quarter.appendChild(select_option_month);
    }
    var month7=d.getMonth()+1;
    $("#select_quarter").val(month7)
    if($('.select_quarter3') && $('.select_quarter3').hasClass('multi')){
        $('.select_quarter3').select2({closeOnSelect:false,multiple:true})
    }
    
    common_request();
}
// 月季年更换
$('.select_option0').on('change', function () {
    if ($(this).children('option')[0].selected == true) {
        $("#select_quarter").hide();
        $(".ui-multiselect").hide();
        $("#select_month").empty();
        for(var i=2017;i<2030;i++){
            var option3=document.createElement("option");
            option3.setAttribute("value",i);
            option3.innerHTML=i;
            select_month.appendChild(option3);
        }
    } else if($(this).children('option')[1].selected == true){
        // $("#select_quarter").hide();
        $("#select_quarter").show();
        $("#select_quarter").empty();
        $("#select_month").empty();
        $(".ui-multiselect").show();
        for(var i=2017;i<2030;i++){
            var option3=document.createElement("option");
            option3.setAttribute("value",i);
            option3.innerHTML=i;
            select_month.appendChild(option3);
        }
        for(var i=1;i<5;i++){
            var option1=document.createElement("option");
            option1.setAttribute("value",i);
            option1.innerHTML=i+"季度";
            select_quarter.appendChild(option1)
        }
        // $("#select_quarter").multiselect("refresh")

    }else if($(this).children('option')[2].selected == true){
        $("#select_quarter").show();
        $("#select_quarter").empty();
        $(".ui-multiselect").show();
        $("#select_month").empty();
        for(var i=2017;i<2030;i++){
            var option3=document.createElement("option");
            option3.setAttribute("value",i);
            option3.innerHTML=i;
            select_month.appendChild(option3);
        }
        for(var i=1;i<13;i++){
            var option2=document.createElement("option");
            option2.setAttribute("value",i);
            option2.innerHTML=i+"月";
            select_quarter.appendChild(option2)
        }
        // $("#select_quarter").multiselect("refresh")
    }
});

$('.headRight>li').eq(3).on('mouseover',function(){
    $('.head_tip').removeClass('none');  
})
$('.headRight>li').eq(3).on('mouseout',function(){
    $('.head_tip').addClass('none');
})
//表格内容为null时，显示空
function showlist(){
    var text_null=$(".shaow_detail_title>td");
    // console.log(text_null)
    $.each(text_null,function(index,obj){
        var textNull=$(this).text();
        if($(this).text()=="null"){
            $(this).text("");
        }
    })
}

//表格翻页 
//翻页 
function ajax_data(p){    
    common_request(p);   
}
//翻页 跳转
function pageIndex(elm){
    var _val = $(elm).prev('input[name=page_index]').val() || $('.changePage').val();
    var p = Number(_val) == NaN?1:Number(_val)
    common_request(p);
}
function chkInit(url,bkfn){
    $.post(url,{sel:1},function(res){
        var _data= res.data;
        var _txt = '',_length=_data.length,_minL=_length>9?9:_length
        $.each(_data,function(index,elm){
            if(index > _minL){
                _txt += '<label for="grid'+elm.id+'"><input type="checkbox" name="checkbox" id="grid'+elm.id+'" value="'+elm.id+'" >'+elm.text+'</label>'
            }else{
                _txt += '<label><input type="checkbox" name="checkbox" id="grid'+elm.id+'" value="'+elm.id+'" checked>'+elm.text+'</label>'
            }
            
        })
        $('.count_classify').html(_txt);
        if(typeof(bkfn) == 'function'){
            bkfn()
        }
        
    },'json')

}
/* 
    // function chkInit(){
    //     $.post(url.base+url.grid,{sel:1},function(res){
    //         var _data= res.data;
    //         var _txt = ''
    //         $.each(_data,function(index,elm){
    //             if(index > 9){
    //                 _txt += '<label for="grid'+elm.id+'"><input type="checkbox" name="checkbox" id="grid'+elm.id+'" value="'+elm.id+'" >'+elm.text+'</label>'
    //             }else{
    //                 _txt += '<label><input type="checkbox" name="checkbox" id="grid'+elm.id+'" value="'+elm.id+'" checked>'+elm.text+'</label>'
    //             }
                
    //         })
    //         $('.count_classify').html(_txt)
    //     },'json')
    // }
*/
(function($){
    
    $.fn.createTable=function(opts){
        var $sObj= this
        $sObj.args = $.extend({
            ids:false,
            data:[],
            columns:[],
            ver:'beta 1.0.0.0'/*版本*/
        },opts||{});
        $sObj.createTh=function(item){
            
        };
        $sObj.createTd=function(type,item,ids){
            var tr = $('<tr class="shaow_detail_title count_tr"></tr>')
            // if($sObj.args.ids){
            //     tr.append($('<td>'+(index+$sObj.args.ids)+'</td>'))
            // }
            $.each($sObj.args.columns, function (index, column) {
                var td = $('<td></td>');
                if(column.ids){
                    td.html(ids+Number(column.ids))
                }else{
                    if(column.field == ''){
                        if(column.render){
                            td.html(column.render(ids,undefined,item,tr))
                        }else{
                            td.html('')    
                        }
                    }else{
                        if(column.render){
                            td.html(column.render(ids,item[column.field],item,tr))
                        }else{
                            td.html(item[column.field])    
                        }
                    }
                }
                if(column.cls){
                    td.addClass(column.cls)
                }
                if(column.w){
                    td.attr('width',column.w)
                }
                if(column.style){
                    td.css(column.style)
                }
                 tr.append(td);
            });
            return tr
        }
        $sObj.creatTable=function(){
            var _thead = $('<thead></thead>')
            //console.log($sObj.find('thead'))
            if($sObj.find('thead').length == 0){
                var tr = $('<tr class="shaow_detail_title count_tr"></tr>')
                $.each($sObj.args.columns, function (index, column) {
                    var td = $('<th></th>');
                    if(column.name){
                        td.html(column.name)
                    }else{
                        td.html('')
                    }
                    tr.append(td);
                })
                _thead.append(tr)
                $sObj.append(_thead)
            }
            if($sObj.find('tbody')){
                $sObj.find('tbody').remove()
            }
            var _tbody = $('<tbody></tbody>')
            if($sObj.args.data.length>0){
                $.each($sObj.args.data,function(index,item){
                    _tbody.append($sObj.createTd('td',item,index))   
                })
            }else{
                _tbody.html('<tr><td style=\"text-align:center;\" colspan="'+($sObj.args.columns.length?$sObj.args.columns.length:1)+'">暂无数据</td></tr>')
                
            }
            _tbody.find('tr:even').css('background','white')
            _tbody.find('tr:odd').css('background','#f1f5f8')
            $sObj.append(_tbody)
        }
        $sObj.creatTable()
    }
    $.fn.rpSelect = function(_objs){
        var $sObj = this
        $sObj.args = $.extend({
            isId:false,
            sel:'',
            url:'',
            type:'POST',
            baseData:{sel:1},
            data:{},
            datas:[],
            multiple:true,
            placeholder:'请选择选项',
            valide:'',
            hasAll:false,
            calfn:function(){}
        },_objs||{});
        $sObj.init = function(){
            var _data = $.extend({},$sObj.args.baseData,$sObj.args.data)
            if($sObj.args.datas.length != 0){
                setTimeout(function(){//延迟一下 vue数据更新时就不会出现无法使用的问题了
                    if($sObj.args.hasAll){
                        $sObj.args.datas.splice(0,0,{id:'none',text:'全部'})
                    }
                    
                    $sObj.select2({
                        placeholder:$sObj.args.placeholder,
                        data:$sObj.args.datas,
                        multiple: $sObj.args.multiple,
                        closeOnSelect: !$sObj.args.multiple
                    })
                    $sObj.args.calfn()
                },0)
                
            }else{
                $.ajax({
                    url:$sObj.args.url,
                    type:$sObj.args.type,
                    data:_data,
                    dataType:"json",
                    success:function(res){
                        if(res.code == 0){
                            $sObj.args.datas = res.data
                            if($sObj.args.hasAll){
                                $sObj.args.datas.splice(0,0,{id:'none',text:'全部'})
                            }   
                        }
                        $sObj.select2({
                            placeholder:$sObj.args.placeholder,
                            data:$sObj.args.datas,
                            multiple: $sObj.args.multiple,
                            closeOnSelect: !$sObj.args.multiple
                        })
                        if($sObj.args.valide && $sObj.args.valide != ''){
                            $sObj.on("select2:close", function (e) {
                                var _check = $sObj.args.valide.check(false,$(e.currentTarget))
                            });
                            $sObj.on("select2:select", function (e) {
                                var _check = $sObj.args.valide.check(false,$(e.currentTarget))
                            });
                            $sObj.on("select2:unselect", function (e) {
                                $sObj.args.valide.check(false,$(e.currentTarget))
                            });
                        }
                        
                        
                    },
                    complete:function(res){
                        $sObj.args.calfn()
                    },
                    error:function(res){
                        console.log(res)
                    }    
                })
            }
            // if($sObj.args.multiple){
            //     $(($sObj.args.isId?'#':'.') + $sObj.args.sel).on("select2:select", function (e) {
            //         // $(($sObj.args.isId?'#':'.') + $sObj.args.sel).select2('open')
            //     });
            //     $(($sObj.args.isId?'#':'.') + $sObj.args.sel).on("select2:close", function (e) {
                    
            //     });
            // }
        }
        $sObj.init()
    }
})(jQuery)
//选择下拉列表 select2版
// (function($){
if(typeof rpSelect == "undefined"){
    if(typeof window.jQuery == "undefined"){
        throw('由于没有加载jQuery库，所以本功能不能使用，请加载jQuery库，谢谢合作！');
    }
    function rpSelect(_objs){
        var $sObj = this
        $sObj.args = $.extend({
            isId:false,
            sel:'',
            url:'',
            type:'POST',
            baseData:{sel:1},
            data:{},
            datas:[],
            multiple:true,
            placeholder:'请选择选项',
            valide:'',
            hasAll:false
        },_objs||{});
        $sObj.init = function(){
            var _data = $.extend({},$sObj.args.baseData,$sObj.args.data)
            if($sObj.args.datas.length != 0){
                setTimeout(function(){//延迟一下 vue数据更新时就不会出现无法使用的问题了
                    if($sObj.args.hasAll){
                        $sObj.args.datas.splice(0,0,{id:'none',text:'全部'})
                    }
                    $(($sObj.args.isId?'#':'.') + $sObj.args.sel).select2({
                        placeholder:$sObj.args.placeholder,
                        data:$sObj.args.datas,
                        multiple: $sObj.args.multiple,
                        closeOnSelect: !$sObj.args.multiple
                    })
                },0)
                
            }else{
                $.ajax({
                    url:$sObj.args.url,
                    type:$sObj.args.type,
                    data:_data,
                    dataType:"json",
                    success:function(res){
                        if(res.code == 0){
                            $sObj.args.datas = res.data
                            if($sObj.args.hasAll){
                                $sObj.args.datas.splice(0,0,{id:'none',text:'全部'})
                            }   
                        }
                        $(($sObj.args.isId?'#':'.') + $sObj.args.sel).select2({
                            placeholder:$sObj.args.placeholder,
                            data:$sObj.args.datas,
                            multiple: $sObj.args.multiple,
                            closeOnSelect: !$sObj.args.multiple
                        })
                        if($sObj.args.valide && $sObj.args.valide != ''){
                            $(($sObj.args.isId?'#':'.') + $sObj.args.sel).on("select2:close", function (e) {
                                var _check = $sObj.args.valide.check(false,$(e.currentTarget))
                            });
                            $(($sObj.args.isId?'#':'.') + $sObj.args.sel).on("select2:select", function (e) {
                                var _check = $sObj.args.valide.check(false,$(e.currentTarget))
                            });
                            $(($sObj.args.isId?'#':'.') + $sObj.args.sel).on("select2:unselect", function (e) {
                                $sObj.args.valide.check(false,$(e.currentTarget))
                            });
                        }
                        
                        
                    },
                    complete:function(res){
                        
                    },
                    error:function(res){
                        console.log(res)
                    }    
                })
            }
            // if($sObj.args.multiple){
            //     $(($sObj.args.isId?'#':'.') + $sObj.args.sel).on("select2:select", function (e) {
            //         // $(($sObj.args.isId?'#':'.') + $sObj.args.sel).select2('open')
            //     });
            //     $(($sObj.args.isId?'#':'.') + $sObj.args.sel).on("select2:close", function (e) {
                    
            //     });
            // }
        }
        $sObj.init()

    }
}
// )(jQuery)

